require "import"
import "tools"
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS).setStatusBarColor(0xff4DB6AC)
activity.setContentView("aMix_")
amix.background=btBackground
oDir.text=LUA_SD.."/avHandle/output/"
format={"aac","mp2","ts","m4a","ogg","wma","wav","flac","ac3","pcm","aiff"}
adp=ArrayAdapter(activity,android.R.layout.simple_spinner_item, String(format))
adp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
sp.Adapter=adp
item=
{
  LinearLayout;
  gravity="center|left";
  orientation="vertical";
  layout_height="50dp";
  layout_width="-1";
  {
    TextView;
    layout_marginLeft="20dp";
    text="路径";
    id="dir";
  };
  {
    LinearLayout;
    layout_marginLeft="20dp";
    {
      TextView;
      text="延时";
      id="tip";
    };
    {
      TextView;
      layout_marginLeft="10dp";
      id="delay";
    };
  };
};

adapter=LuaAdapter(activity,item)
adapter.add{dir="点击添加音频文件",tip=""}
lv.Adapter=adapter
lv.onItemClick=function(a,b,c,d)
  local s= b.Tag.dir.text
  if s=="点击添加音频文件"
    ChoiceFile(LUA_SD,function(p)
      table.insert(adapter.data,#adapter.data,{dir=p,tip="延时",delay="0秒"})
      adapter.notifyDataSetChanged()
    end)
   else
    local items={"删除","预览","延时"}
    AlertDialog.Builder(this) .setItems(items,{onClick=function(l,v)
        local str=items[v+1]
        if str=="删除"
          table.remove(adapter.data,d)
          adapter.notifyDataSetChanged()
        end
        if str=="预览"
          dialogPlayer(s)
        end
        if str=="延时"
          AlertDialog.Builder(this) .setTitle("请输入延时") .setView(loadlayout({EditText,id="et";hint="单位为秒"})).setPositiveButton("确定", {onClick=function()
              adapter.data[d].delay=et.text.."秒"
              adapter.notifyDataSetChanged()
            end}).setNegativeButton("取消", nil).show()
        end
      end}).show()
  end
end
oDir.onClick=function()
  ChoicePath(LUA_SD,function(dir)
    oDir.text=dir.."/"
  end)
end
amix. onClick=function()

  if #adapter.data>1
    if filename.text~=""
      local input="-i "
      local filter_complex=""
      local tag=""
      for n=1,#adapter.data-1 do
        if n==#adapter.data-1
          input=input..adapter.data[n].dir.." "
         else
          input=input..adapter.data[n].dir.." -i "
        end
        local seconds=(adapter.data[n].delay):gsub("秒","")
        if filter_complex==""
          filter_complex=filter_complex.. "["..tostring(n-1).."]adelay=".. tostring(tonumber(seconds)*1000).."|"..tostring(tonumber(seconds)*1000).."[tag".. tostring(n).."]"
         else
          filter_complex=filter_complex.. ",["..tostring(n-1).."]adelay=".. tostring(tonumber(seconds)*1000).."|"..tostring(tonumber(seconds)*1000).."[tag".. tostring(n).."]"
        end
        tag=tag.."[tag".. tostring(n).."]"
      end
      tag=tag.."amix=".. tostring(#adapter.data-1)
      local cmd=input.."-filter_complex "..filter_complex..","..tag.." -y "
      if tostring(sp.getSelectedItem())=="pcm"
        cmd=cmd.."-f s16le "
      end
      cmd=cmd ..oDir.text..filename.text.."."..sp.getSelectedItem()
      activity.newActivity("handle",{cmd})
     else
      print("请输入文件名")
    end
   else
    print("请选择文件")
  end
end